import { generateUniqueId } from "utils/utils";
import { EntityDB } from "../EntityDB";
import { EntityFactory } from "../EntityFactory";
import { EntityType } from "../Entitys/Entity.interface";
import { Autoplainning } from "Autoplanning/Autoplainning";
import { UtilsHelper } from "utils/position/UtilsHelper";
import { TeamUpgrader } from "../Entitys";
const TeamTypes = new Set<EntityType>(["Team2","Team9","TeamUpgrader","Team4","TeamDefender"])
const RoomEntityConfig:Record<number,EntityType[]>={
1:["InnerMineEntity","MinerialEntity","SpawnEntity","CoreEntity","LabEntity"],
2:["InnerMineEntity","MinerialEntity","SpawnEntity","CoreEntity","LabEntity"],
3:["InnerMineEntity","MinerialEntity","SpawnEntity","CoreEntity","LabEntity"],
4:["InnerMineEntity","MinerialEntity","SpawnEntity","CoreEntity","LabEntity"],
5:["InnerMineEntity","MinerialEntity","SpawnEntity","CoreEntity","LabEntity"],
6:["InnerMineEntity","MinerialEntity","SpawnEntity","CoreEntity","LabEntity"],
7:["InnerMineEntity","MinerialEntity","SpawnEntity","CoreEntity","LabEntity"],
8:["InnerMineEntity","MinerialEntity","SpawnEntity","CoreEntity","LabEntity"],
};
/**
 * 主防策略
 */
export class MainStrategy
{
    public static initRoom(room:Room,entityDB:EntityDB)
    {
        if(room.controller?.level)
        {
            this.checkConfig(room.controller?.level,entityDB,room.name);
            this.recoverFromMemory(room as Required<Room>,entityDB);
        }
    }
    public static check(room:Room,entityDB:EntityDB)
    {
        if(room.controller?.level)
        {
            this.checkConfig(room.controller?.level,entityDB,room.name);
        }
        
    }

    private static checkConfig(level:number,entityDB:EntityDB,roomName:string)
    {
 
        const entitys = RoomEntityConfig[level];
        for(const e of entitys)
        {
            const plancache  = global.world.PollBlueplan(roomName);
            if(plancache)
            {
               
                const Plans =  plancache.getModulePlan(e);
                for(const plan of Plans)
                {
                    const guid = `${roomName}_${plan.type}`;
                     
                    if(entityDB.getEntityByID(guid))
                    {
                        continue;
                    }
                
                    const entity = EntityFactory.CreateModule(e,{
                        GUID:`${roomName}_${plan.type}`,
                        key:plan.type,
                        roomName:roomName,
                        plan:plan
                    },entityDB);
                    entityDB.add(entity);
                }
            }
            else
            {
                global.Log.Error(`错误!找不到${ roomName} 的布局蓝图`);
            }
        }
    }

    // 从Memory中读取数据恢复 实体对象
    private static recoverFromMemory(room:Required<Room>,entityDB:EntityDB)
    {
       
        const memorys = room.memory.Nodes  ;
        for(let k in memorys)
        {
            const type = memorys[k].type  as EntityType;
            if(type == "OutMineEntity")
            {
                if(memorys[k].skey)
                {
                    const obj = UtilsHelper.deSerializeObject(memorys[k].skey);
                    const entity = EntityFactory.CreateModule("OutMineEntity",{
                        GUID:k,
                        key:k,
                        roomName:obj.pos.roomName,
                    },entityDB);
                    entityDB.add(entity);
                }
                else 
                {
                    global.Log.Error(`初始化 ${JSON.stringify(memorys[k])}错误`);
                }
                
                
            }
            else if(type == "OutMinerialEntity")
            {
                if(memorys[k].skey)
                {
                    const obj = UtilsHelper.deSerializeObject(memorys[k].skey);
                    const entity = EntityFactory.CreateModule("OutMinerialEntity",{
                        GUID:k,
                        key:k,
                        roomName:obj.pos.roomName,
                    },entityDB);
                    entityDB.add(entity);
                }
                else 
                {
                    global.Log.Error(`初始化 ${JSON.stringify(memorys[k])}错误`);
                }
            }
            else if(TeamTypes.has(type))
            {
                global.Log.Info(`恢复实体${type}:${JSON.stringify(memorys[k])}`);
                const entity = EntityFactory.CreateModule(type,{
                    GUID:k,
                    key:k,
                    roomName:memorys[k].region,
                },entityDB);
                
                entityDB.add(entity);
                const teamTask = Game.rooms[entity.region].findTaskById((entity as TeamUpgrader).getNodeMemory().bindTask!,"team") as ITaskDeclare["team"];
                if(teamTask)
                {
                    const index = teamTask.data.teams[type].findIndex(e=>e == entity.GUID());
                    if(index==-1)
                    {
                        entityDB.removeEntityByID(entity.GUID());
                        // entity.onDestory();
                    }
                }
            }
        }
    }
}